package com.sunday.pinxinghui.model.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.pinxinghui.MainActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.model.mine.SetPayPwdActivity;
import com.sunday.pinxinghui.network.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/14.
 */

public class MobileLoginActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.et_phone)
    ClearEditText etPhone;
    @Bind(R.id.et_pwd)
    ClearEditText etPwd;
    @Bind(R.id.iv_reset_pwd_visible)
    ImageButton ivResetPwdVisible;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    @Bind(R.id.btn_register)
    TextView btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mobile_login);
        ButterKnife.bind(this);
        titleView.setText("密码登录");
    }


    private boolean isPressed = true;

    @OnClick({R.id.iv_reset_pwd_visible, R.id.btn_register, R.id.btn_login, R.id.btn_forget_pwd})
    void click(View v) {
        switch (v.getId()) {
            case R.id.iv_reset_pwd_visible:
                if (!isPressed) {
                    ivResetPwdVisible.setSelected(false);
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivResetPwdVisible.setSelected(true);
                }
                isPressed = !isPressed;
                etPwd.postInvalidate();
                CharSequence text = etPwd.getText();
                if (text instanceof Spannable) {
                    Spannable spanText = (Spannable) text;
                    Selection.setSelection(spanText, text.length());
                }
                break;
            case R.id.btn_login:
                String mobile1 = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile1) || !StringUtils.isMobileNO(mobile1)) {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                    return;
                }
                String pwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }
                Call<ResultDO<Member>> call = ApiClient.getApiAdapter().login(1, mobile1, null, MD5Utils.MD5(pwd)
                        , null, null, null, null);
                call.enqueue(new Callback<ResultDO<Member>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        ResultDO<Member> resultDO = response.body();
                        if (response.body().getCode() == 0) {
                            SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                            SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                            Member member = response.body().getResult();
                            BaseApplication.getInstance().setMember(member);
                            intent = new Intent(mContext, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });
                break;
            case R.id.btn_register:
                intent=new Intent(mContext,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_forget_pwd:
                intent = new Intent(mContext, SetPayPwdActivity.class);
                intent.putExtra("updateType",3);
                startActivity(intent);
                break;
        }
    }


}
